package mo.tags.greedy;

import java.util.Arrays;
import java.util.Comparator;

public class L435 {

    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length == 0) {
            return 0;
        }
        Arrays.sort(intervals, Comparator.comparingInt(i -> i[1]));
        int select = 1;
        int n = intervals.length;
        int right = intervals[0][1];
        for (int i = 1; i < n; i++) {
            if (intervals[i][0] >= right) {
                select++;
                right = intervals[i][1];
            }
        }
        return n - select;
    }

}
